/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.ProfessionDict;
import me.zhengjie.modules.biz.domain.req.UniversitiesRecommendReq;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.ProfessionDictService;
import me.zhengjie.modules.biz.domain.vo.ProfessionDictQueryCriteria;
import me.zhengjie.modules.biz.mapper.ProfessionDictMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-03-11
**/
@Service
@RequiredArgsConstructor
public class ProfessionDictServiceImpl extends ServiceImpl<ProfessionDictMapper, ProfessionDict> implements ProfessionDictService {

    private final ProfessionDictMapper professionDictMapper;

    @Override
    public PageResult<ProfessionDict> queryAll(ProfessionDictQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(professionDictMapper.findAll(criteria, page));
    }

    @Override
    public List<ProfessionDict> queryAll(ProfessionDictQueryCriteria criteria){
        return professionDictMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ProfessionDict resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProfessionDict resources) {
        ProfessionDict professionDict = getById(resources.getId());
        professionDict.copy(resources);
        saveOrUpdate(professionDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<ProfessionDict> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProfessionDict professionDict : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("专业代码(专科)", professionDict.getSpecialistCode());
            map.put("专科专业", professionDict.getSProfessionName());
            map.put("专业代码(本科)", professionDict.getUndergraduateCode());
            map.put("本科专科", professionDict.getUProfessionName());
            map.put("考试科目", professionDict.getExamSubject());
            map.put("删除【0：未删除 1：已删除】", professionDict.getIsDelete());
            map.put("创建时间", professionDict.getCreateTime());
            map.put("修改时间", professionDict.getUpdateTime());
            map.put("备注", professionDict.getRemark());
            map.put("考试类别", professionDict.getExamType());
            map.put("划线类别", professionDict.getLineType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 智能荐校，查询推荐专业的列表
     * @param req
     * @return
     */
    @Override
    public List<String> recommendUniversities(UniversitiesRecommendReq req) {
        return professionDictMapper.recommendUniversities(req);
    }
}